package org.jet.emall.rbac.service;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.jet.emall.rbac.dto.AdminMenuDTO;
import org.jet.emall.rbac.po.AdminMenu;
import org.jet.emall.rbac.po.AdminPrivilegeMenu;
import org.jet.emall.rbac.po.AdminRolePrivilege;
import org.jet.emall.rbac.service.mp.IAdminMenuService;
import org.jet.emall.rbac.service.mp.IAdminPrivilegeMenuService;
import org.jet.emall.rbac.service.mp.IAdminRolePrivilegeService;
import org.jet.emall.rbac.transfer.AdminMenuDtoTransfer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author xiaozai
 * @version 1.0
 * @date 2020-04-05 16:49
 */
@Service
public class CustomAdminMenuServiceImpl implements CustomAdminMenuService{

    @Autowired
    private IAdminMenuService adminMenuService;

    @Autowired
    private IAdminRolePrivilegeService adminRolePrivilegeService;

    @Autowired
    private IAdminPrivilegeMenuService adminPrivilegeMenuService;



    @Override
    public List<AdminMenuDTO> list(Integer roleId) {

        //调私有方法拿到菜单ID列表
        List<Integer> ids = getMenuPrivilegeIdsByRoleId(roleId);
        //先查出所有菜单
        List<AdminMenu> adminMenuList = null;
        // 如果
        if(ids!=null&&!ids.isEmpty()) {
            adminMenuList = adminMenuService.listByIds(ids);
        }else{
            return null;
        }
        //将所有菜单构成的列表处理成树形
        return  buildMenuTree(adminMenuList);
    }

    @Override
    public AdminMenuDTO getMenuByName(String name) {
        Wrapper<AdminMenu> wrapper = new QueryWrapper<>();
        ((QueryWrapper<AdminMenu>) wrapper).eq("name",name);
        AdminMenu adminMenu = adminMenuService.getOne(wrapper);
        AdminMenuDTO adminMenuDTO = AdminMenuDtoTransfer.INSTANCE.toDto(adminMenu);
        return adminMenuDTO;
    }

    /**
     *
     * 将所有菜单构建成一树形结构
     * @param adminMenuList 所有菜单的线性列表
     * @return 树形菜单列表
     * */
    private List<AdminMenuDTO> buildMenuTree(List<AdminMenu> adminMenuList){

        Map<Integer,AdminMenuDTO> map = new HashMap<>();
        //将所有菜单转成dto并以id为key放进map中
        adminMenuList.forEach(value->{
            AdminMenuDTO dto = AdminMenuDtoTransfer.INSTANCE.toDto(value);
            map.put(dto.getId(),dto);
        });

        List<AdminMenuDTO> result = new LinkedList<>();
        //再对map中的menu做处理，就是设置好子菜单列表
        map.forEach((key,value)->{
            if(value.getSuperId()!=0){
                AdminMenuDTO superAdminMenu = map.get(value.getSuperId());
                List<AdminMenuDTO> children = superAdminMenu.getChildren();
                if(children==null){
                    children = new LinkedList<>();
                    superAdminMenu.setChildren(children);
                }
                children.add(value);
            }else{
                result.add(value);
            }
        });

        map.clear();

        return result;
    }

    /**
     *  获取角色的菜单ID列表
     * @param roleId
     * @return
     */
    private List<Integer> getMenuPrivilegeIdsByRoleId(Integer roleId){


        Wrapper<AdminRolePrivilege> wrapper = new QueryWrapper<>();
        ((QueryWrapper<AdminRolePrivilege>) wrapper).eq("rid",roleId);
        List<AdminRolePrivilege> list = adminRolePrivilegeService.list(wrapper);
        if(list==null||list.isEmpty()){
            return null;
        }

        List<Integer> pids = list.stream().mapToInt(value -> value.getPid()).collect(ArrayList<Integer>::new, List::add, List::addAll);

        Wrapper<AdminPrivilegeMenu> wrapper2 = new QueryWrapper<>();
        ((QueryWrapper<AdminPrivilegeMenu>) wrapper2).in("pid",pids);
        List<AdminPrivilegeMenu> list1 = adminPrivilegeMenuService.list(wrapper2);
        if(list1==null || list1.isEmpty()){
            return null;
        }
        List<Integer> mids = list1.stream().mapToInt(value -> value.getMid()).collect(ArrayList<Integer>::new, List::add, List::addAll);
        return mids;
    }

}
